package com.thrc.borrow.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.thrc.borrow.dao.CourseDao;
import com.thrc.borrow.entity.Course;
import com.thrc.borrow.entity.Order;
import com.thrc.borrow.vo.CourseVo;
import com.thrc.borrow.vo.OrderVo;

@Service
public class CourseService implements BaseService<Course, CourseVo>{

	@Autowired
	private CourseDao courseDao;
	
	@Override
	public CourseVo selectById(Course t) {
		return courseDao.selectById(t);
	}

	@Override
	public List<CourseVo> selectAll(Course t) {
		return courseDao.selectAll(t);
	}

	@Override
	public int selectCount(Course t) {
		return courseDao.selectCount(t);
	}

	@Override
	public List<CourseVo> selectByPage(Course t) {
		return courseDao.selectByPage(t);
	}

	@Override
	public void update(Course t) {
		courseDao.update(t);
	}

	@Override
	public void delete(Course t) {
		courseDao.delete(t);
	}

	@Override
	public void insert(Course t) {
		courseDao.insert(t);
	}

	public void insertOrder(Order t){
		courseDao.insertOrder(t);
	}
	
	public List<OrderVo> selectByCondition(Order t){
		return courseDao.selectByCondition(t);
	}
	
	/**
	 * 回调购买课程
	 * @param openid   openid
	 * @param out_trade_no 订单流水号
	 * @return
	 */
	public int buyCourse(String openid,String out_trade_no){
		Order t=new Order();
		t.setRemark(openid);
		t.setTradeNo(out_trade_no);
		List<OrderVo> orderList=courseDao.selectByCondition(t);
		if(orderList.size()==1){
			t=orderList.get(0);
			t.setUpdateTime(new Date());
			t.setState("success");
			courseDao.updateOrder(t);
		}
		return 1;
	}

}
